<?php
require_once 'includefiles.php';
require_once 'model/trade.php';
require_once 'model/users.php';
require_once 'model/budget.php';
require_once 'model/testimonials.php';
require_once 'model/state.php';
include_once("model/blogs.php");

if($_REQUEST['pre'])
{
	$vars = array();
	foreach($_REQUEST as $key=>$value)
		$vars[] = $key.'='.$value;


	switch(trim($_REQUEST['pre']))
	{
		case "myaccount":
			redirect_to_link(SITE_URL.'/myaccount/paymentreturn/?'.implode("&",$vars));	
			break;

		case "lead":
			redirect_to_link(SITE_URL.'/leads/paymentreturn/?'.implode("&",$vars));	
			break;
			
		case "registration":
		default:
			redirect_to_link(SITE_URL.'/register/paymentreturn/?'.implode("&",$vars));	
			break;
	}
	exit;
}



$userObj = new Model_Users();
$budgetObj = new Model_Budget();
$objTrade = new Model_TRADE();
$objtestimonial = new Model_Testimonial();
$stateObj = new Model_State();
$blogsObj = new Model_Blogs();

$limit = 2;
$offset = ($page-1) * $limit;
$blogs = $blogsObj->getAllBlogs($limit,0);

$smarty->assign('blogs',$blogs);
$tradejobtypeArray = $objTrade->getAllJobTypeWithActiveStatus();
foreach($tradejobtypeArray as $tradejobtypeArray)
{	
	$newarray[] =	$tradejobtypeArray['parent_id'];
}

## Fetch all active states
$stateArray = $stateObj->getAllStateWithActiveStatus();
//echo "<pre>"; print_r($stateArray); exit;
$smarty->assign('stateArray', $stateArray);
	
$tradecategoryArray = array_values(array_unique($newarray));
if($tradecategoryArray[0]!='')
{
		$imopldeids = @implode(",",$tradecategoryArray);
		$newtradecatArray = $objTrade->getAllTradesCategoryWithmultipleId($imopldeids);
}
//echo "<pre>";print_R($getAlltestimonialArray);die();
$smarty->assign("newtradecategoryArray", $newtradecatArray);

if($_GET['tradetype']!='')
{
	
	$tradecatname = str_replace("-"," ",$_GET['tradetype']);
	$tradecatname1 = str_replace("and","&",$tradecatname);
	$gettradedetail = $objTrade->checkTradeCatNameExits($tradecatname1);
	//echo "<pre>";print_R($gettradedetail['id']);die();
	$tradecatid = $gettradedetail['id'];
	$smarty->assign("tradecatid", $tradecatid);
	if($tradecatid!='')
	{
		$new_tradejobtypeArray = $objTrade->getAllJobtypeByTradeId($tradecatid);	
		$smarty->assign("new_tradejobtypeArray", $new_tradejobtypeArray);
	}
}

$getallactivebudgetArray = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign("getallactivebudgetArray", $getallactivebudgetArray);

$gettestimonialArray = $objtestimonial->LastTestimonialDetails();
if($gettestimonialArray['user_id']!='')
{
	$getuserdetail = $userObj->getUserDetailsByUserId($gettestimonialArray['user_id']);
}
$gettestimonialArray['username'] =$getuserdetail['first_name'].' '.$getuserdetail['last_name'] ;
$smarty->assign("gettestimonialArray", $gettestimonialArray);
$getAlltestimonialArray = $objtestimonial->getAllTestimonialDetails();
//echo "<pre>";print_R($getAlltestimonialArray);die();
$smarty->assign("counttestimonialArray", count($getAlltestimonialArray));

$jobArray = $userObj->getTwoDescJobArray();
$smarty->assign("jobArray", $jobArray);

$AllReviewUser = $userObj->getallReviewUser();
for($i=0;$i<count($AllReviewUser);$i++)
{
	/*if($AllReviewUser[$i]['tradeid']!='0')
	{
		$gettradetype = $objTrade->getTradeCategoryDetailsById($AllReviewUser[$i]['tradeid']);	
		$AllReviewUser[$i]['trade_name'] = $gettradetype['trade_category'];
	}*/
	if($AllReviewUser[$i]['workarea']!='')
	{
		$getlocation = $stateObj->getPostalCodeDetailsByPostalId($AllReviewUser[$i]['workarea']);
		$AllReviewUser[$i]['location'] = $getlocation['suburb'];	
	}
	if($AllReviewUser[$i]['avgrating'])
	{
			$total_rating = $AllReviewUser[$i]['avgrating'];
			$AllReviewUser[$i]['total_rating'] =$total_rating;

			if($total_rating < 5){
				$remaining_rating = 5 - $total_rating;
				$AllReviewUser[$i]['remaining_rating'] =$remaining_rating;

			} 
			else
			{
				$AllReviewUser[$i]['total_rating'] =0;	
				$AllReviewUser[$i]['remaining_rating'] =5;
			}
	}
	
	$AllReviewUser[$i]['compname'] =str_replace(" ","_",trim($AllReviewUser[$i]['companyname'])); 
}
$smarty->assign("AllReviewUser", $AllReviewUser);
//echo "<pre>";print_R($AllReviewUser);die();
if($_SESSION['getbids_id']!='')
{
	$userdetail = $userObj->getUserDetailsByUserId($_SESSION['getbids_id']);
	$smarty->assign("userdetail", $userdetail);
}

if($_SESSION['msg']!=''){
	$smarty->assign("successMsg", $_SESSION['msg']);
	unset($_SESSION['msg']);
}
if($_SESSION['error'] != ''){
	$smarty->assign("errorMsg", $_SESSION['error']);	
	unset($_SESSION['error']);
}

$smarty->display(TEMPLATEDIR . '/index.tpl');
unset($smarty);
?>